import requests
import logging

from api.ihrmEmpManageApi import EmpManageApi
from common.assertTools import common_assert
from common.db_tools import DBTools
from common.getHeader import get_header
from config import TEL


class TestAddEmp(object):

    def setup_class(self):
        # 创建实例
        self.emp = EmpManageApi()
        # 准备请求头
        self.req_header = get_header()

    def setup(self):  # 在每个普通方法执行前,自动调用(先删除数据库手机号)
        DBTools.db_iud(f"delete from bs_user where mobile='{TEL}';")

    def teardown(self):  # 测试结束后,自动调用删除这个手机号
        DBTools.db_iud(f"delete from bs_user where mobile='{TEL}';")

    # 添加成功
    def test1_add_success(self):
        # 准备请求体
        req_body = {"username": "张好", "mobile": TEL, "workNumber": "013"}
        # 调用添加员工
        resp = self.emp.add_emp(self.req_header, req_body)
        # print("添加成功：", resp.json())
        # 代替print
        logging.info(f"添加成功:{resp.json()}")
        # 断言
        common_assert(resp, 200, True, 10000, "操作成功")

    # 手机号为空
    def test2_mobile_nulls(self):
        # 准备请求体
        req_body = {"username": "张好", "mobile": None, "workNumber": "013"}
        # 调用添加员工
        resp = self.emp.add_emp(self.req_header, req_body)
        # print("手机号为空：", resp.json())
        # 代替print
        logging.info(f"手机号为空:{resp.json()}")
        # 断言
        common_assert(resp, 200, False, 20002, "新增员工失败")
